const axios = require('axios')
const path = require('path')
const fs = require('fs')
const execSync = require("child_process").execSync

const baseURL = `http://172.29.4.144:9187`

// 获取医院字典
const getHosDict = () => {
    return axios.get('/nansha/frontEnd/hosNameCodeDict', {baseURL})
}

// 南沙区公立医疗机构运营实况
const getScreen0Data = () => {
    return axios.get('/nansha/frontEnd/screenTwo', {baseURL})
}

// 有几个数据需要实时刷新
const getRealtimeData = () => {
    return axios.get('/nansha/frontEnd/screenTwoInTime', {baseURL})
}

// 南沙区公立医疗机构基本情况概览
const getScreen1Data = () => {
    return axios.get('/nansha/frontEnd/screenThree', {baseURL})
}

// 获取急救人数
const getFirstAids = () => {
    return axios.get('/nansha/firstAids', {baseURL: 'http://172.29.4.144:9188'})
}

// ------ 新冠 -------

const instance = axios.create({
    // baseURL: 'http://39.108.123.58:8082/v1.0',
    baseURL: 'http://172.29.4.144:8092/v1.0',  // 生产环境
});

// 响应拦截器
instance.interceptors.response.use(response => {
    return response.data
}, (err) => {
    return Promise.reject(err)
})


// 医院防疫情况
const getDayInfo = (params) => {
    return instance.get('/ncp/getDayInfo', {params})
}

// 获中间主指标，四类人员分布，去向分析
const get_mainData = (params) => {
    return instance.get('/ncp/mainData', {params})
}

// 获取发热门诊人数排名
const get_diagnoseNumRank = (params) => {
    return instance.get('/ncp/diagnoseNumRank', {params})
}

// 疫情趋势分析
const get_trends = (params) => {
    return instance.get('/ncp/trends', {params})
}

// 医疗机构字典表
const get_orgDict = (params) => {
    return instance.get('/ncp/orgDict', {params})
}

/**
 * 数据录入接口
 */

const dataGet = (id) => {
    return instance.get(`/data/${id}`, {})
}

const dataList = (projectId = 1, params) => {
    return instance.get(`/data/${projectId}/page`, {params,})
}

/**
 * 复合接口
 */
const getDataByName = (name) => {
    console.log('name ', name)
    return dataList(undefined, {
        current: 1,
        limit: 20,
        sort: 'createTime',
        direction: 'desc',
        name
    }).then(res => {
        if (res.data.items.length !== 0) {
            return Promise.all(res.data.items.map(item => dataGet(item.id)))
        }
        const errMsg = `${name} 数据表找不到`
        return Promise.reject(new Error(errMsg))
    })
}


async function start() {
    let ret

    ret = await getHosDict()
    fs.writeFileSync(path.join(__dirname, `./save/hosNameCodeDict.json`), JSON.stringify(ret.data, null, 4))
    ret = await getScreen0Data()
    fs.writeFileSync(path.join(__dirname, `./save/screenTwo.json`), JSON.stringify(ret.data, null, 4))

    ret = await getRealtimeData()
    fs.writeFileSync(path.join(__dirname, `./save/screenTwoInTime.json`), JSON.stringify(ret.data, null, 4))

    ret = await getScreen1Data()
    fs.writeFileSync(path.join(__dirname, `./save/screenThree.json`), JSON.stringify(ret.data, null, 4))

    ret = await getFirstAids()
    fs.writeFileSync(path.join(__dirname, `./save/firstAids.json`), JSON.stringify(ret.data, null, 4))


    //
    let finalSaveData = await Promise.all([
        getDataByName('{物资}'),
        getDataByName('{固定}'),
        getDataByName('{确诊人数}'),
        getDataByName('{中心医院}'),
        getDataByName('{社区}'),
        getDayInfo(),
        get_mainData(),
        get_diagnoseNumRank(),
        get_trends(),
        get_orgDict()
    ]).then(([
                 stock,
                 resistData,
                 diagnosis,
                 centerHos,
                 shequArr,
                 {data: dayInfo},
                 {data: mainData},
                 {data: diagnoseNumRank},
                 {data: trends},
                 {data: orgDict}
             ]) => {
        return {
            stock: stock[0].data.content.data,
            resistData: resistData[0].data.content.data,
            diagnosis: diagnosis[0].data.content.data,
            centerHos: centerHos[0].data.content.data,
            shequArr: shequArr.map(v => v.data),
            dayInfo,
            mainData,
            diagnoseNumRank,
            trends,
            orgDict
        }
    })


    fs.writeFileSync(path.join(__dirname, `./save/ncov.json`), JSON.stringify(finalSaveData, null, 2))

    console.log('-------- 下载成功 --------')

    execSync(`git pull`)
    execSync(`git add .`)
    execSync(`git commit -m "${Date.now()}"`)
    execSync(`git push https://wuyanwuyan:a460605745@gitee.com/wuyanwuyan/nansha_json.git --all`)

}

start()